package org.darkforest.merchant.controller;

import org.darkforest.common.services.ConsultService;
import org.darkforest.common.services.MessageAndNoticeService;
import org.darkforest.common.services.RefundReturnService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.JacksonsUtil;
import org.darkforest.controller.WebController;
import org.darkforest.dao.mapper.RefundReturnMapper;
import org.darkforest.merchant.service.OrderService;
import org.darkforest.model.*;
import org.darkforest.ucenter.auth.AuthPassport;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by liu on 15/3/18.
 */
@Controller
@RequestMapping(value = "/customer")
public class CustomerController extends WebController<CustomerController> {
    @Autowired
    private ConsultService consultService;
    @Autowired
    private RefundReturnService refundReturnService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private RefundReturnMapper refundReturnMapper;
    @Autowired
    private MessageAndNoticeService messageAndNoticeService;

    /**
     * 负责售后服务的方法
     * TODO 用于咨询方面的
     * @return
     */
    @RequestMapping(value="consultList")
    public ModelAndView consultList(HttpServletRequest request,ConsultSearchBean page) throws Exception {
        List consultTypeList = consultService.getConsultTypeList();
        ModelAndView modelAndView = new ModelAndView("customer/consultList","consultTypeList",consultTypeList);
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        page.setStoreId(sellerVo.getStoreId());
        modelAndView.addObject("page", page);
        consultService.selectListByCondition(page);
        return modelAndView;
    }

    @RequestMapping(value="getConsultById")
    public ModelAndView getConsultById(HttpServletRequest request,int consultId,String searchType,int ctId) throws Exception {
        Consult consult = consultService.getConsultById(consultId);
        ConsultSearchBean page = new ConsultSearchBean();
        page.setSearchType(searchType);
        page.setCtId(ctId);
        ModelAndView modelAndView = new ModelAndView("customer/updateConsult","consult",consult);
        modelAndView.addObject("page", page);
        return modelAndView;
    }


    @RequestMapping(value="updateConsultById")
    public ModelAndView getConsultById(Consult consult,ConsultSearchBean page) throws Exception {
        consult.setConsultReplyTime(DateUtil.unixTimeStampInt().intValue());
        int x = consultService.updateConsult(consult);

        //咨询回复提醒
        consult = consultService.getConsultById(consult.getConsultId());
        messageAndNoticeService.sendConsultGoodsReplyToUser(consult);
        return new ModelAndView("redirect:/customer/consultList?searchType="+page.getSearchType()+"&&ctId="+page.getCtId());
    }


    @RequestMapping(value="deleteConsultByIds")
    public ModelAndView deleteConsultByIds(ConsultSearchBean page,String [] ids) throws Exception {
        if(ids!=null) {
            List nList = Arrays.asList(ids);
            consultService.deleteByIds(nList);
        }
        return new ModelAndView("redirect:/customer/consultList?searchType="+page.getSearchType()+"&&ctId="+page.getCtId());
    }






    /**
     * 用于退款的
     * @return
     */
    @RequestMapping(value="refundList")
    public ModelAndView refundList(RefundReturnSearchBean page,HttpServletRequest request) throws Exception {
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        page.setStoreId(sellerVo.getStoreId());
        page.setRefundType(1);
        refundReturnService.selectListByCondition(page);
        System.out.println(page.getCount());
        return new ModelAndView("customer/refundList","page", page);
    }

    /**
     * 用于退款的处理
     * @return
     */
    @RequestMapping(value="refundDetail")
    public ModelAndView refundDetail(int refundId) throws Exception {
        Map<String, Object> p = new HashMap<String, Object>();
        p.put("refundId", refundId);
        RefundReturnVo refund = refundReturnMapper.selectById(p);
//        RefundReturn refund = refundReturnService.getRefundReturnById(refundId);
        OrderVo orderVo= orderService.selectById(refund.getOrderId());
        OrderCommonVo commonVo = orderVo.getCommon();
        if(!"".equals(commonVo.getReciverInfo())&&commonVo.getReciverInfo()!=null) {
            Address address = JacksonsUtil.newInstance().json2Obj(commonVo.getReciverInfo(), Address.class);
            commonVo.setAddress(address);
        }
        ModelAndView m =  new ModelAndView("customer/refundDetail","refund",refund);
        m.addObject("orderVo",orderVo);
        return m;
    }

    /**
     * 用于退款的处理
     * @return
     */
    @RequestMapping(value="sellerOpetateRefund")
    public ModelAndView sellOpetateRefund(RefundReturn refund) throws Exception {
        refund.setSellerTime(DateUtil.unixTimeStampInt().intValue());
        int result =  refundReturnService.updateRefundReturn(refund);
        RefundReturn newfund = refundReturnService.getRefundReturnById(refund.getRefundId());
        messageAndNoticeService.sendRefundReturnNoticeToUser(newfund);
        //更新订单lock值
        if(newfund.getSellerState() != null &&newfund.getSellerState().intValue() ==3) {
            Order order = new Order();
            order.setOrderId(newfund.getOrderId());
            order.setLockState(0);
            orderService.updateByPrimaryKeySelective(order);
        }

        return new ModelAndView("customer/refundDetail","refund", newfund);
    }





    /**
     * 用于退货的。
     * @return
     */

    @RequestMapping(value="rebackList")
    public ModelAndView rebackList(RefundReturnSearchBean page,HttpServletRequest request) throws Exception {
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        page.setStoreId(sellerVo.getStoreId());
        page.setRefundType(2);
        refundReturnService.selectListByCondition(page);
        System.out.println(page.getCount());
        return new ModelAndView("customer/rebackList","page", page);
    }

    @RequestMapping(value="rebackDetail")
    public ModelAndView rebackDetail(int refundId) throws Exception {
        Map<String, Object> p = new HashMap<String, Object>();
        p.put("refundId", refundId);
        RefundReturnVo refund = refundReturnMapper.selectById(p);
//        RefundReturn refund = refundReturnService.getRefundReturnById(refundId);
        OrderVo orderVo= orderService.selectById(refund.getOrderId());
        OrderCommonVo commonVo = orderVo.getCommon();
        if(!"".equals(commonVo.getReciverInfo())&&commonVo.getReciverInfo()!=null) {
            Address address = JacksonsUtil.newInstance().json2Obj(commonVo.getReciverInfo(), Address.class);
            commonVo.setAddress(address);
        }
        ModelAndView m =  new ModelAndView("customer/rebackDetail","refund",refund);
        m.addObject("orderVo",orderVo);
        return m;

    }


    /**
     * 用于退款的处理
     * @return
     */
    @RequestMapping(value="sellerOpetateReback")
    public ModelAndView sellerOpetateReback(RefundReturn refund) throws Exception {
        refund.setSellerTime(DateUtil.toUnixTimeStamp(DateUtil.toTimeString(new Date())));
        refundReturnMapper.updateByPrimaryKeySelective(refund);
        RefundReturn newfund = refundReturnService.getRefundReturnById(refund.getRefundId());
        //退款退货发送消息
//        if(newfund.getRefundType() ==2){
            messageAndNoticeService.sendRefundReturnNoticeToUser(newfund);
//        }
        //更新订单lock值
        if(newfund.getSellerState() != null &&newfund.getSellerState().intValue() ==3) {
            Order order = new Order();
            order.setOrderId(newfund.getOrderId());
            order.setLockState(0);
            orderService.updateByPrimaryKeySelective(order);
        }


        return new ModelAndView("customer/rebackDetail","refund", newfund);
    }



    /*
     * 确认收货展示
     */
    @RequestMapping(value="toConfirmDeliverAjax")
    @AuthPassport
    @ResponseBody
    public ModelAndView toConfirmDeliver(int refundId) throws Exception {
        RefundReturn refund = refundReturnService.getRefundReturnById(refundId);
        ModelAndView m =  new ModelAndView("customer/confirm_deliver","refund",refund);
        return m;

    }


    /*
    * 确认收货操作
    */
    @RequestMapping(value="doConfirmDeliver")
    public String doConfirmDeliver(RefundReturn refund) throws Exception {
        refund.setSellerTime(DateUtil.toUnixTimeStamp(DateUtil.toTimeString(new Date())));
        refund.setReceiveTime(DateUtil.toUnixTimeStamp(DateUtil.toTimeString(new Date())));
        refund.setSellerState(2);
        refund.setRefundState(2);
        refund.setGoodsState(4);
        refundReturnMapper.updateByPrimaryKeySelective(refund);

        //退款退货发送消息
        refund = refundReturnService.getRefundReturnById(refund.getRefundId());
        if(refund.getRefundType() ==2){
            messageAndNoticeService.sendRefundReturnNoticeToUser(refund);
        }

        return "redirect:/customer/rebackList";
    }

}
